package com.example.primomobile;

import java.util.Map;

import android.content.ClipData;
import android.graphics.drawable.Drawable;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.DragShadowBuilder;
import android.view.View.OnTouchListener;
import android.widget.ImageView;

import com.example.primomobile.utils.Utils;

public class RemoverPecaListener implements OnTouchListener{


	@Override
	public boolean onTouch( View view, MotionEvent motionEvent ) {

		if ( view.getTag() != null && (Boolean) ( (Map) view.getTag() ).get( Utils.REMOVE ) ) {
			if ( motionEvent.getAction() == MotionEvent.ACTION_DOWN ) {
				ClipData data = ClipData.newPlainText( "", "" );

				Map<String, Object> tag = (Map) view.getTag();
				Drawable aux = ( (ImageView) view ).getDrawable();

				if ( ( (Map) view.getTag() ).containsKey( Utils.PECA ) ) {
					String peca = (String) ( (Map) view.getTag() ).get( Utils.PECA );
					if ( peca.equals( Utils.DIREITA ) ) {
						( (ImageView) view ).setImageResource( R.drawable.direita );
					} else if ( peca.equals( Utils.ESQUERDA ) ) {
						( (ImageView) view ).setImageResource( R.drawable.esquerda );
					} else if ( peca.equals( Utils.FRENTE ) ) {
						( (ImageView) view ).setImageResource( R.drawable.frente );
					} else if ( peca.equals( Utils.FUNCAO ) ) {
						( (ImageView) view ).setImageResource( R.drawable.funcao );
					}
				}

				DragShadowBuilder shadowBuilder = new View.DragShadowBuilder( view );
				view.startDrag( data, shadowBuilder, view, 0 );

				((ImageView)view).setImageResource( R.drawable.img_default1 );

				tag.put( Utils.REMOVE, false );
				tag.put( Utils.PECA_TROCA, false );
				return true;
			} else {
				return false;
			}
		}
		return false;
	}
}
